// 引入config中的url(后面点什么就是 什么环境)
const apiUrl = "https://z3929e8970.51vip.biz/"
// http 请求
let urlList = ['login','user/register']
 const httpTokenRequest = (options) => {
	let promise = new Promise(function(resolve, reject) {
        let obj = {
            url: apiUrl  + options.url || '', // 请求地址拼接的路径
			data: options.data || {},
			method: options.method || 'POST',
			header: {
			}, //设置请求头
			responseType: options.responseType || "text",
			timeout: 15000,
			success(res) {
				let flag = Interceptor(res)
				if (flag) return;
				resolve(res.data);
			},
			fail(res) {
				reject(res);
			}
		
        }
        console.log(options.url);
        console.log(urlList);
        if(!urlList.includes(options.url)){
            let TOKENS = wx.getStorageSync('TOKENS') || "";
            obj.header['Authorization'] = TOKENS
        }
		wx.request(obj)
	})
	return promise
};



// 拦截器 （可自定义）不需要可以注释
function Interceptor(res) {
    let flag = false
    console.log(res.data);
	if (res.data) {
		if (res.data.result == 403) {
			wx.clearStorage();
			wx.reLaunch({
				url: '/pages/login/login'
			})
			wx.showToast({
				title: '请先登录',
				icon: 'none'
			})
			flag = true
		}
		
	}

	return flag
}

export default httpTokenRequest